﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Eixos3D
{
    class GameObject
    {

        Matrix world;
        Model modelo;

        /// <summary>
        /// O efeito que será utilizado para fazer o desenho 3D
        /// </summary>
        private BasicEffect effect;


        /// <summary>
        /// A matriz que representa as transformações do mundo virtual. São 3: pra rotação, translação e escala
        /// </summary>
        public Matrix rotationMatrix = Matrix.Identity;
        public Matrix scaleMatrix = Matrix.Identity;
        public Matrix translationMatrix = Matrix.Identity;



        public GameObject(Model modelo)
        {
            //Matriz mundo
            this.world = this.rotationMatrix * this.scaleMatrix * this.translationMatrix;

            //Objeto
            this.modelo = modelo;
        }



        public void Update(KeyboardState keyboard)
        {
            // Checagem de tecla pressionada para rotacionar, escalar ou transladar o objeto 3D
            if (keyboard.IsKeyDown(Keys.R))
            {
               rotationMatrix *= Matrix.CreateRotationX(0.01f) * Matrix.CreateRotationY(0.01f);

            }


            if (keyboard.IsKeyDown(Keys.E) && keyboard.IsKeyDown(Keys.PageUp))
            {
                scaleMatrix *= Matrix.CreateScale(1.01f);
            }

            if (keyboard.IsKeyDown(Keys.E) && keyboard.IsKeyDown(Keys.PageUp))
            {
                scaleMatrix *= Matrix.CreateScale(1.01f);
            }

            else if (keyboard.IsKeyDown(Keys.E) && keyboard.IsKeyDown(Keys.PageDown))
            {
                scaleMatrix *= Matrix.CreateScale(0.99f);
            }



            if (keyboard.IsKeyDown(Keys.T))
            {
                if (keyboard.IsKeyDown(Keys.Left))
                {
                    translationMatrix *= Matrix.CreateTranslation(-0.05f, 0, 0);
                }

                if (keyboard.IsKeyDown(Keys.Right))
                {
                    translationMatrix *= Matrix.CreateTranslation(0.05f, 0, 0);
                }

                if (keyboard.IsKeyDown(Keys.Up))
                {
                    translationMatrix *= Matrix.CreateTranslation(0, 0.05f, 0);
                }

                if (keyboard.IsKeyDown(Keys.Down))
                {
                    translationMatrix *= Matrix.CreateTranslation(0, -0.05f, 0);
                }

            }
        }


        /// <summary>
        /// Classe Draw usando câmeras livres
        /// </summary>

        public void Draw(Camera camera)
        {
            this.world = this.rotationMatrix * this.scaleMatrix * this.translationMatrix;
            this.modelo.Draw(world, camera.view, camera.projection);

        }
    }
}
